import matplotlib.pyplot as plt
import math
from random import random
import blackbox

def blackBox(y):
	points = map(lambda x: blackbox.Point(x[1],x[0]), y)
	return blackbox.BlackBox().calculateTime(points)

bestFit = 9999999.
bestY = list()
history = []
bestHist = []
bestA = 999999.
bestB = 999999.
a=0
b=0
size = 10
for i in range(0,1000):
	a = -100*random()
	b = -a + size
	y = range(0, size+1)
	y = map(lambda q: -math.sqrt(-q*q+2*q*a+b*b)+b, y)
	points = []
	for x in range(0,size+1):
		points.append([x,y[x]])
	newFit = blackBox(points)
	history += [newFit]
	bestHist += [bestFit]
	if newFit < bestFit:
		bestFit = newFit
		bestY = list(points)
		bestA = a
		bestB = b

#print bestY
print "Circle equation: (x+" + str(-bestA) + ")+(y-" + str(bestB) + ") = " + str(bestA*bestA+bestB*bestB)
print "Radius: " + str(math.sqrt(bestA*bestA+bestB*bestB))
print blackBox(bestY)
x = range(0, size+1)
plt.plot(map(lambda s: s[1], bestY))
plt.show()
plt.plot(history)
plt.show()
bestHist[0] = bestHist[1]
plt.plot(bestHist)
plt.show()